'''
Created on 03.12.2009

@author: stud
'''

import vtk
import math
import time
from Tkinter import *
from vtk.tk.vtkTkRenderWidget import *


global entry
global lichtStatus
global lichtLabel

def ok():
    global entry
    print entry.get()

def ende():
    root.destroy()    

if __name__ == '__main__':
    root = Tk()
    root.title('Mehrkoerpersystem') 
   
    entryFrame = Frame(root)
    
    ipEntry = Entry(entryFrame)
    ipEntry.pack(side = TOP)
    portEntry = Entry(entryFrame)
    portEntry.pack(side = TOP)
    massminEntry = Entry(entryFrame)
    massminEntry.pack(side = TOP)
    massmaxEntry = Entry(entryFrame)
    massmaxEntry.pack(side = TOP)
    radiminEntry = Entry(entryFrame)
    radiminEntry.pack(side = TOP)
    radimaxEntry = Entry(entryFrame)
    radimaxEntry.pack(side = TOP)
    xEntry = Entry(entryFrame)
    xEntry.pack(side = TOP)
    yEntry = Entry(entryFrame)
    yEntry.pack(side = TOP)
    zEntry = Entry(entryFrame)
    zEntry.pack(side = TOP)
    centralStarMassEntry = Entry(entryFrame)
    centralStarMassEntry.pack(side = TOP)
    
    entryFrame.pack(side = RIGHT)

    
    labelFrame = Frame(root)
    
    ipLabel = Label(labelFrame, text="IP:")
    ipLabel.pack(side=TOP, ipady = 1)
    portLabel = Label(labelFrame, text="Port:")
    portLabel.pack(side= TOP, ipady = 1)
    minmassLabel = Label(labelFrame, text = "Min_Mass:")
    minmassLabel.pack(side= TOP, ipady = 1)
    maxmassLabel = Label(labelFrame, text = "Max_Mass:")
    maxmassLabel.pack(side= TOP, ipady = 1)
    radiminLabel = Label(labelFrame, text = "Min_Radius:")
    radiminLabel.pack(side= TOP, ipady = 1)
    radimaxLabel = Label(labelFrame, text = "Max_Radius:")
    radimaxLabel.pack(side= TOP, ipady = 1)
    xLabel = Label(labelFrame, text = "Ausdehnung_x:")
    xLabel.pack(side= TOP, ipady = 1)
    yLabel = Label(labelFrame, text = "Ausdehnung_y:")
    yLabel.pack(side= TOP, ipady = 1)
    zLabel = Label(labelFrame, text = "Ausdehnung_z:")
    zLabel.pack(side= TOP, ipady = 1)
    CentralStarLabel = Label(labelFrame, text = "Masse_Zentralgestirn:")
    CentralStarLabel.pack(side= TOP, ipady = 1)
    
    labelFrame.pack(side = RIGHT, expand = False)
    
 
    
    
    
   
    sphere = vtk.vtkSphereSource()
    sphere.SetRadius(1.0)                   #dort eingreifen
    sphere.SetPhiResolution(20)
    sphere.SetThetaResolution(20)
    sphereMapper = vtk.vtkPolyDataMapper()
    sphereMapper.SetInputConnection(sphere.GetOutputPort())
    sphereActor = vtk.vtkActor()
    sphereActor.SetMapper(sphereMapper)
    ren = vtk.vtkRenderer()
    ren.AddActor(sphereActor)
    ren.SetBackground(0.1, 0.2, 0.4)
    light = vtk.vtkLight()
    light.SetPosition(- 200, - 200, 200)    # hier muss Zentralgesirn hin
    ren.AddLight(light)
    cam = ren.GetActiveCamera()
    cam.SetPosition(0, -70, 0)
    cam.SetFocalPoint(0, 0, 0)
    cam.SetViewUp(0, 0, 1)
    cam.SetViewAngle(40)
    ren.SetActiveCamera(cam)

    renderWidget = vtkTkRenderWidget(root, width = 400, height = 400)
    ren.SetBackground(0.1, 0.2, 0.4)
    renderWindow = renderWidget.GetRenderWindow()
    renderWindow.AddRenderer(ren)
    renderWindow.SetSize(300, 300)
    #
    loopRunning = False
    exitRequested = False
    currentTime=0.0
    timeStep=0.01
    #
    # quit function
    def quit():
        global exitRequested, loopRunning, rootTk, vis
        if loopRunning:
            exitRequested = True    
        else:
            rootTk.quit()
    #
    # sphere loop
    def sphereLoop():
        global loopRunning, rootTk, exitRequested
        loopRunning = True
        while not exitRequested:                        
            global vis, currentTime, timeStep    
            time.sleep(timeStep)
            currentTime += timeStep
            pX, pZ = 20. * math.cos(currentTime), 20. * math.sin(currentTime) 
            sphereActor.SetPosition(pX, 0, pZ)
            renderWindow.Render()
            rootTk.update()
        rootTk.quit()                                  
    #
    renderWidget.pack(expand = 'true', fill = 'both')       # hier auf eigene Tk umbiegen
    #
    quitButton = Button(root, text = "Quit", command = quit)
    quitButton.pack(expand = 'true', fill = 'x')
    startButton = Button(root, text = 'Start', command = sphereLoop)
    startButton.pack(expand = 'true', fill = 'x')
    #
    rootTk.mainloop()
